package com.example.petback.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.petback.mapper.OrderMapper;
import com.example.petback.model.OrderModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    public List<OrderModel> getOrdersByStatus(Integer status){
        QueryWrapper<OrderModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",status);
        return orderMapper.selectList(queryWrapper);
    }
    public List<OrderModel> getOrdersByItemid(String likeitemid)
    {
        QueryWrapper<OrderModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("itemid",likeitemid);
        return orderMapper.selectList(queryWrapper);
    }
    public OrderModel getOrderByNumber(String number){
        QueryWrapper<OrderModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("number",number);
        return orderMapper.selectOne(queryWrapper);
    }

    public void getOrdersPage(Integer num, Integer size){
        QueryWrapper<OrderModel> queryWrapper = new QueryWrapper<>();
        Page<OrderModel> page = new Page<>(num,size);
        Page<OrderModel> orderModelPage = orderMapper.selectPage(page, queryWrapper);
        System.out.println(orderModelPage.getTotal());
        System.out.println(orderModelPage.getPages());
        System.out.println(orderModelPage.getRecords());
    }

    public void updateOrderByNumber(String address,String name,String phone,String number){
        UpdateWrapper<OrderModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("number",number);
        updateWrapper.set("address",address);
        updateWrapper.set("name",name);
        updateWrapper.set("phone",phone);
        orderMapper.update(new OrderModel(),updateWrapper);
    }

    public void updateCompanyByNumber(String number,String company){
        UpdateWrapper<OrderModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("number",number);
        updateWrapper.set("company",company);
        orderMapper.update(new OrderModel(),updateWrapper);
    }

    public void updateStatus(String number){
        UpdateWrapper<OrderModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("number", number);
        updateWrapper.set("status",1);
        orderMapper.update(new OrderModel(),updateWrapper);
    }

}
